<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 快捷方式控制器
 * @package app\index\controller
 */
class Quick extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取快捷键列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_quick')
                ->field('id,name,first_key,second_key')
                ->where(['store_id'=>$this->userInfo['store_id'],'user_id'=>$this->userInfo['id']])
                ->order('id asc')
                ->select();
            $resultData = json_encode($data, JSON_UNESCAPED_UNICODE);
            $this->result('查询成功', $resultData, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加快捷键
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['data'])){
                $this->result('缺少参数', '', 204);
            }
            $quickRes = json_decode($getData['data'],true);
            //dump($quickRes);die;
            //删除原有数据然后再添加
            Db::name('qt_quick')->where(['store_id'=>$this->userInfo['store_id'],'user_id'=>$this->userInfo['id']])->delete();
            //添加快捷方式
            $insert = [];
            foreach ($quickRes as $key=>$value){
                if(!isset($value['second_key']) || empty($value['second_key']) ){
                    $value['second_key'] = '';
                }
                if(!isset($value['first_key']) || empty($value['first_key']) ){
                    $value['first_key'] = '';
                }
                $insert[$key] = [
                    'name'=>$value['name'],
                    'first_key'=>$value['first_key'],
                    'second_key'=>$value['second_key'],
                    'user_id'=>$this->userInfo['id'],
                    'store_id'=>$this->userInfo['store_id'],
                    'create_time'=>time(),
                ];
            }

            $insert = Db::name('qt_quick')->insertAll($insert);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑单个快捷方式
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_quick')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     *获取快捷键列表
     * @return array
     */
    public function quick_list()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_quick')
                ->field('id,name,first_key,second_key,create_time')
                ->where(['store_id'=>$this->userInfo['store_id'],'user_id'=>$this->userInfo['id']])
                ->order('id asc')
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

}
